package com.bzkj.basicinfo.service.impl.ncc;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bzkj.basicinfo.domain.ncc.IcTranstypetostate;
import com.bzkj.basicinfo.mapper.IcTranstypetostateMapper;
import com.bzkj.basicinfo.service.ncc.IcTranstypetostateService;
import com.bzkj.basicinfo.utils.DataBatchProcessorUtil;
import com.bzkj.basicinfo.utils.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName IcTranstypetostateServiceImpl
 * @Description TODO
 * @Author C1309
 * @Date 2025/6/17 17:38
 */
@Service("nccIcTranstypetostateServiceImpl")
public class IcTranstypetostateServiceImpl implements IcTranstypetostateService {
    private static final Logger log = LoggerFactory.getLogger(IcTranstypetostateServiceImpl.class);
    @Resource
    private IcTranstypetostateMapper icTranstypetostateMapper;

    @Value("${pk_org.code}")
    private Integer pkOrgCode;

    @Autowired
    private HttpClientUtils hcu;
    @Override
    public void jobMethod(Boolean flag) {
        try {
            HashMap<String, String> searchDate = DataBatchProcessorUtil.getSearchDate(flag);

            // 查询当前数据
            HttpResponse response = hcu.methodExecute("2025-01-13 02:32:20", "2025-03-13 02:32:20", 18, pkOrgCode);
            JSONObject jsonNode = JSON.parseObject(response.body());
            // 收集rep系统数据
            List<IcTranstypetostate> list = jsonNode.getJSONArray("data").toJavaList(IcTranstypetostate.class);
            if (!list.isEmpty()) {
                extracted(list);
            }
        } catch (HttpException e) {
            log.error("同步失败:{}", e);
        }
    }

    private void extracted(List<IcTranstypetostate> list) {
        for (IcTranstypetostate icTranstypetostate : list) {
            icTranstypetostateMapper.insertOrUpdate(icTranstypetostate);
        }
    }
}
